package com.hjm.ui.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjm.ui.R;

public class TopBar extends LinearLayout {
    private TextView titleTextView;
    private LinearLayout extendLayout;
    private ImageView backImageView;
    public TopBar(@NonNull Context context) {
        super(context);
        init();
    }

    public TopBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        String text = a.getString(R.styleable.TopBar_titleText);
        Boolean showBack = a.getBoolean(R.styleable.TopBar_showBack, false);
        if (text != null) {
            titleTextView.setText(text);
        }
        if (showBack) {
            backImageView.setVisibility(VISIBLE);
        } else {
            backImageView.setVisibility(GONE);
        }
    }

    public TopBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init(){
        // 在这里添加你的布局和逻辑
        LayoutInflater.from(getContext()).inflate(R.layout.layout_top_bar, this, true);
        titleTextView = findViewById(R.id.text_view_top_bar_title);
        extendLayout = findViewById(R.id.layout_top_bar_extend);
        backImageView = findViewById(R.id.image_view_top_bar_back);


    }

    public void setTitle(String title){
        titleTextView.setText(title);
    }

    public void setExtendLayout(View view){
        extendLayout.addView(view);
    }

    public void setOnBackClickListener(OnClickListener listener){
        backImageView.setOnClickListener(listener);
    }
}
